# coding: utf-8
# @Author: 健聪
# @Description: 飞机大战
# @Date: 2024/3/9
# pip3 install pynput

from pynput.keyboard import Key, Listener, KeyCode

# 设计默认背景
background = [[0, 0, 0],
              [0, "x", 0],
              [0, 0, 0]]
# print(background[0])
# print(background[1])
# print(background[2])
for i in background:
    print(i)


def on_press(key):
    if key == KeyCode.from_char('w'):
        # print("你按下了 上 键")
        for i in 0, 1, 2:
            for j in 0, 1, 2:
                # 飞机向上移动时，需要判断是否超出或者到了了第一行（0列）
                if background[i][j] == "x" and i > 0:
                    background[i][j] = "0"
                    background[i - 1][j] = "x"
                    break

    elif key == KeyCode.from_char('s'):
        # print("你按下了 下 键")
        for i in 2, 1, 0:
            for j in 0, 1, 2:
                if background[i][j] == "x" and i < 2:
                    background[i][j] = "0"
                    background[i + 1][j] = "x"
                    break

    elif key == KeyCode.from_char('a'):
        # print("你按下了 左 键")
        for i in 0, 1, 2:
            for j in 0, 1, 2:
                if background[i][j] == "x" and i > 0:
                    background[i][j] = "0"
                    background[i][j - 1] = "x"
                    break

    elif key == KeyCode.from_char('d'):
        # print("你按下了 右 键")
        for i in 2, 1, 0:
            for j in 0, 1, 2:
                if background[i][j] == "x" and i < 2:
                    background[i][j] = "0"
                    background[i][j + 1] = "x"
                    break
    print()
    # 输出新的变化结果
    for i in background:
        print(i)

# 监听键盘操作
with Listener(on_press=on_press) as listener:
    listener.join()
